package com.qf;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/8/11 10:25
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @RequestMapping("/test1")
    public String test() {
        return "访问到了search微服务, 并且成功执行!";
    }


    /**
     * 测试post方式提交, 这样接收参数
     * @param user
     * @return
     */
    @RequestMapping("/insert")
    public String insertUser(User user) {
        System.out.println("=======" + user);
        return "保存成功!";
    }

    /**
     * 测试使用restful方式提交参数
     * @return
     */
    @RequestMapping("/findByName/{name}")
    //指定该方法一旦报错, 使用findByNameFallBack方法进行兜底
    @HystrixCommand(fallbackMethod = "findByNameFallBack",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "5"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000")
    })
    public String findByName(@PathVariable(value = "name") String name) {
        System.out.println("=======" + name);
        //模拟异常报错信息
        int i = 1/0;
        return "查询成功!";
    }

    /**
     * 兜底方法和被兜底方法, 传参, 返回值要一样, 方法名不一样
     * @param name
     * @return
     */
    public String findByNameFallBack(String name){
        return "调用出错, 被hystrix进行兜底, 这里是search微服务兜底";
    }
}